package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemMsg;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.SystemMsgMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceimpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private SystemMsgMapper msgMapper;

    @Override
    public PageInfo getPageInfo(TravelQuery qo) {
        //"t.createTime desc"
        //PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),"createTime desc");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderby());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Long save(Travel travel) {
        //设置创建时间
        travel.setCreateTime(new Date());
        //设置编辑时间
        travel.setLastUpdateTime(new Date());

        travelMapper.insert(travel);
        //游记内容的保存
        TravelContent travelContent = travel.getTravelContent();
        travelContent.setId(travel.getId());
        travelContentMapper.insert(travelContent);
        return travel.getId();

    }

    @Override
    public Travel get(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        //把内容也查询出来设置进游记中一并返回
        TravelContent travelContent = travelContentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    @Override
    public void update(Travel travel) {
        //更新时 默认值需要更新
        travel.setLastUpdateTime(new Date());
        System.out.println(travel);
        //游记的更新操作
        travelMapper.updateByPrimaryKey(travel);
        //游记的内容也需要同步更新
        TravelContent tc = travel.getTravelContent();
        tc.setId(travel.getId());
        travelContentMapper.updateByPrimaryKey(tc);

    }

    @Override
    public void changeState(Travel ravel) {
        //建立一个系统信息实体类--小智
        SystemMsg msg = new SystemMsg();
        msg.setMsgDate(new Date());
        msg.setTravel(ravel);
        Travel travel = travelMapper.selectByPrimaryKey(ravel.getId());
        msg.setMsger(travel.getAuthor());
        //----------------------
        //如果是发布状态 需要添加一个发布时间
        if (ravel.getState() == Travel.STATE_RELEASE) {
            ravel.setReleaseTime(new Date());
            //----小智 系统通知
            msg.setContent("您的游记已被发布");
        } else if (ravel.getState() == Travel.STATE_REJECT) {
            msg.setContent("您的游记已被拒绝");
        } else if (ravel.getState() == Travel.STATE_WAITING) {
            msg.setContent("您的游记已进入待审核阶段");
        }
        msgMapper.insert(msg);
        //-----------------
        travelMapper.updateState(ravel);
    }

    @Override
    public TravelContent getTravelContentById(Long travelId) {
        return travelContentMapper.selectByPrimaryKey(travelId);
    }

    @Override
    public PageInfo queryStrategyTravel(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.selectStrategyTravel(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Travel> achieveStrategyTravelsByStrategyId(Long strategyId) {
        TravelQuery travelQuery = new TravelQuery();
        travelQuery.setStrategyId(strategyId);
        List<Travel> travels = travelMapper.selectStrategyTravel(travelQuery);
        for (Travel travel : travels) {
            Integer count = travelMapper.selectCollectCountForTravel(travel.getId());
            travelMapper.updateCollectCountForStrategyTravel(strategyId, travel.getId(), count);
        }
        return travelMapper.selectStrategyTravelsByStrategyId(strategyId);
    }
}
